package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.customer.persist.Merchant;
import com.checkfree.isolutions.customer.persist.MerchantHomeImpl;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Display an account list for user.
 */
public class CusSelfCareServlet_v1r01 extends EbppxTransactionServlet {

    /**
     * Load vector of accounts into request.
     */
    public void doTransaction(
        HttpServletRequest req,
        HttpServletResponse res)
    throws ServletException {
        try
        {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

            // get accounts from session and filter out nulls
            final Vector vecAccounts = new Vector();
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
            final Vector cfiAccounts = ecsUser.getAccounts();
            final int size = cfiAccounts.size();
            for(int i=0;i<size;i++){
                final Account cfiAccount = (Account) cfiAccounts.elementAt(i);
                if (cfiAccount!=null){
          if(cfiAccount.getProperty("EmailAdd1").equals("blank")){
            cfiAccount.setProperty("EmailAdd1",ecsUser.getEmail());
          }
          if(cfiAccount.getProperty("EmailAdd2").equals("blank")){
            cfiAccount.setProperty("EmailAdd2",ecsUser.getEmail());
          }
          vecAccounts.addElement(cfiAccount);
                }
            }

            Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
            User cfiUser = getUserHome().findByLogonIdAndMerchant(ecsUser.getUserId(),cfiMerchant);
            req.setAttribute("LastUpdateDate", cfiUser.getProperty("LastUpdateDate"));

            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, vecAccounts);
            req.setAttribute(EbppxConstants.SELFCARE_ACKNOWLEDGEMENT, "");
            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        }
        catch (Throwable t)
        {
            throw new ServletException(t.toString(), t);
        }
    }



  /*
  if(!cfiAccount.getProperty("EmailAdd1").equals("blank")){
    strEMailAdd1 = cfiAccount.getProperty("EmailAdd1");
  }
  if(strEMailAdd1.equals("")){
      strEMailAdd1 = ecsu.getEmail();
  }
  if(!cfiAccount.getProperty("EmailAdd2").equals("blank")){
    strEMailAdd2 = cfiAccount.getProperty("EmailAdd2");
  }
  if(strEMailAdd2.equals("")){
    strEMailAdd2 = ecsu.getEmail();
  }

  */
}